package models;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

import java.text.ParseException;
import java.util.Arrays;
import java.util.Date;

import org.junit.Before;
import org.junit.Test;


public class testPayment {
	String date_str = "2011-03-17";
	Date date;
	Client client;
	Payment payment;
	final int PAYMENT_ID = 43;
	
	@Before
	public void setup() throws ParseException {
		date = DatabaseModel.dbDateFormat.parse(date_str);
		client = new Client(1, "Mark", "Spencer", "Myroad 16, 28240 MyCity", "06 390 2432 34", "markspancer@gmail.com");
		payment = new Payment(PAYMENT_ID, client, 100.0, date, "Comment about this Payment");
	}
	
	@Test
	public void testpaymentConstruction() {
		assertEquals(client, payment.getClient());
		assertEquals(100.0, payment.getAmount(), 0.001);
		assertEquals(date, payment.getDate());
		assertEquals("Comment about this Payment", payment.getComment());
	}
	
	@Test
	public void getId() {
		assertEquals(PAYMENT_ID, payment.getId());
	}
	
	@Test
	public void testDataHeaders() {
		String[] expected = {"client", "amount", "date", "comment"};
		assertEquals(Arrays.asList(expected), Arrays.asList(payment.getDataHeaders()));
	}
	
	@Test
	public void testGetData() {
		String[] lijst = {"1", "100.0", date_str, "Comment about this Payment"};
		assertEquals(Arrays.asList(lijst), Arrays.asList(payment.getData()));
	}
	
	@Test
	public void test_getDataString() {
		String expected = "client='1', amount='100.0', date='" + date_str + "', comment='Comment about this Payment'";
		assertEquals(
				expected,	
				payment.getDataString()
		);
	}
	
	@Test
	public void test_getSelectString() {
		assertEquals(
				"client='1' AND amount='100.0' AND date='" + date_str + "' AND comment='Comment about this Payment'",	
				payment.getSelectString()
		);
	}
	
	@Test
	public void testSearchedMatch() {
		assertEquals(true, payment.isSearchMatch("Mark"));
		assertEquals(true, payment.isSearchMatch("100"));
		assertEquals(true, payment.isSearchMatch("Comment about"));
		assertEquals(false,payment.isSearchMatch("ownroad"));
	}
	
	@Test
	public void testEqualsAndHashCode() {
		Client identical_client = new Client(1, "Mark", "Spencer", "Myroad 16, 28240 MyCity", "06 390 2432 34", "markspancer@gmail.com");
		Payment payment_with_identical_client = new Payment(PAYMENT_ID, identical_client, 100.0, date, "Comment about this Payment");
		assertEquals(payment, payment_with_identical_client);
		assertEquals(payment.hashCode(), payment_with_identical_client.hashCode());

		Client different_client = new Client(1, "Mark2", "Spencer", "Myroad 16, 28240 MyCity", "06 390 2432 34", "markspancer@gmail.com");
		Payment payment_with_different_client = new Payment(PAYMENT_ID, different_client, 100.0, date, "Comment about this Payment");
		assertTrue( ! payment.equals(payment_with_different_client));
		assertTrue(payment.hashCode() != payment_with_different_client.hashCode());
	}
}